package model;

import java.util.List;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.hibernate.LockMode;
import org.hibernate.Query;
import org.springframework.context.ApplicationContext;
import org.springframework.orm.hibernate3.support.HibernateDaoSupport;

/**
 * A data access object (DAO) providing persistence and search support for
 * Special entities. Transaction control of the save(), update() and delete()
 * operations can directly support Spring container-managed transactions or they
 * can be augmented to handle user-managed Spring transactions. Each of these
 * methods provides additional information for how to configure it for the
 * desired type of transaction control.
 * 
 * @see model.Special
 * @author MyEclipse Persistence Tools
 */

public class SpecialDAO extends HibernateDaoSupport {
	private static final Log log = LogFactory.getLog(SpecialDAO.class);
	// property constants
	public static final String SPECIALNAME = "specialname";
	public static final String SPECIALDEPARTMENT_ID = "specialdepartmentId";

	protected void initDao() {
		// do nothing
	}

	/**
	 * 根据学院id获得该学院对应的所有专业
	 * @param departmentid
	 */
	public List getAllSpecialByDepartmentId(String departmentid)
	{
		String hql="FROM Special AS s";
		String temp =" WHERE ";
		String clause=temp;
		if(departmentid!=null&&!"".equals(departmentid)&&!departmentid.equals("0"))
		{
			clause=clause+" s.specialdepartmentId=:specialdepartmentId";
		}
		if(!clause.equals(temp))
		{
			hql=hql+" "+clause;
		}
		Query q=super.getSession().createQuery(hql);
		if(departmentid!=null&&!"".equals(departmentid)&&!departmentid.equals("0"))
		{
			q.setInteger("specialdepartmentId", Integer.parseInt(departmentid));
		}
		
		List list = q.list();
		return list;
	}
	
	public void deleteAllSpecialInfo()
	{
		String hql="DELETE FROM Special AS s";
		Query q=super.getSession().createQuery(hql);
		q.executeUpdate();
	}
	
	//get all special info
	public List getAllSpecialInfo()
	{
		String hql="FROM Special AS s," +
				"Department AS d" +
				" WHERE s.specialdepartmentId=d.id";
		Query q=super.getSession().createQuery(hql);
		List list = q.list();
		return list;
	}
	
	//按专业名称搜索专业信息
	public List searchSpecials(String specialname,String specialdepartmentId)
	{
		System.out.println("传来的specialdepartmentId的参数为："+specialdepartmentId);
		String hql="FROM Special AS s," +
			"Department AS d" +
			" WHERE s.specialdepartmentId=d.id ";
		String temp="";
		String clause=temp;
		if(specialname!=null&&!"".equals(specialname))
		{
			clause=clause+" and s.specialname like :specialname";
		}
		if(specialdepartmentId!=null&&!"".equals(specialdepartmentId)&&!specialdepartmentId.equals("0"))
		{
			clause=clause+" and s.specialdepartmentId=:specialdepartmentId";
		}
		if(!clause.equals(temp))
		{
			hql=hql+" "+clause;
		}
		System.out.println("hql语句为："+hql);
		Query q=super.getSession().createQuery(hql);
		if(specialname!=null&&!"".equals(specialname))
		{
			q.setString("specialname", '%'+specialname+'%');
		}
		if(specialdepartmentId!=null&&!"".equals(specialdepartmentId)&&!specialdepartmentId.equals("0"))
		{
			q.setInteger("specialdepartmentId", Integer.parseInt(specialdepartmentId));
		}
		
		System.out.println(hql);
		List searchlist=q.list();
		return searchlist;
	}
	
	
	public void save(Special transientInstance) {
		log.debug("saving Special instance");
		try {
			getHibernateTemplate().save(transientInstance);
			log.debug("save successful");
		} catch (RuntimeException re) {
			log.error("save failed", re);
			throw re;
		}
	}

	public void delete(Special persistentInstance) {
		log.debug("deleting Special instance");
		try {
			getHibernateTemplate().delete(persistentInstance);
			log.debug("delete successful");
		} catch (RuntimeException re) {
			log.error("delete failed", re);
			throw re;
		}
	}

	public Special findById(java.lang.Integer id) {
		log.debug("getting Special instance with id: " + id);
		try {
			Special instance = (Special) getHibernateTemplate().get(
					"model.Special", id);
			return instance;
		} catch (RuntimeException re) {
			log.error("get failed", re);
			throw re;
		}
	}

	public List findByExample(Special instance) {
		log.debug("finding Special instance by example");
		try {
			List results = getHibernateTemplate().findByExample(instance);
			log.debug("find by example successful, result size: "
					+ results.size());
			return results;
		} catch (RuntimeException re) {
			log.error("find by example failed", re);
			throw re;
		}
	}

	public List findByProperty(String propertyName, Object value) {
		log.debug("finding Special instance with property: " + propertyName
				+ ", value: " + value);
		try {
			String queryString = "from Special as model where model."
					+ propertyName + "= ?";
			return getHibernateTemplate().find(queryString, value);
		} catch (RuntimeException re) {
			log.error("find by property name failed", re);
			throw re;
		}
	}

	public List findBySpecialname(Object specialname) {
		return findByProperty(SPECIALNAME, specialname);
	}

	public List findBySpecialdepartmentId(Object specialdepartmentId) {
		return findByProperty(SPECIALDEPARTMENT_ID, specialdepartmentId);
	}

	public List findAll() {
		log.debug("finding all Special instances");
		try {
			String queryString = "from Special";
			return getHibernateTemplate().find(queryString);
		} catch (RuntimeException re) {
			log.error("find all failed", re);
			throw re;
		}
	}

	public Special merge(Special detachedInstance) {
		log.debug("merging Special instance");
		try {
			Special result = (Special) getHibernateTemplate().merge(
					detachedInstance);
			log.debug("merge successful");
			return result;
		} catch (RuntimeException re) {
			log.error("merge failed", re);
			throw re;
		}
	}

	public void attachDirty(Special instance) {
		log.debug("attaching dirty Special instance");
		try {
			getHibernateTemplate().saveOrUpdate(instance);
			log.debug("attach successful");
		} catch (RuntimeException re) {
			log.error("attach failed", re);
			throw re;
		}
	}

	public void attachClean(Special instance) {
		log.debug("attaching clean Special instance");
		try {
			getHibernateTemplate().lock(instance, LockMode.NONE);
			log.debug("attach successful");
		} catch (RuntimeException re) {
			log.error("attach failed", re);
			throw re;
		}
	}

	public static SpecialDAO getFromApplicationContext(ApplicationContext ctx) {
		return (SpecialDAO) ctx.getBean("SpecialDAO");
	}
}